import logging
from qiniu import Auth, put_data



access_key = 'ReL16IVHa_X-BsFwy_zGVtbPpdI5N8QFKRYf6Sad'
secret_key = "7BDFu55tC-8X0Pp4SaoERbLqHPz5kbb78n2MuJKz"

# 要上传的空间
bucket_name = 'hehua'


def storage(data):
    """七牛云上传接口"""

    if not data:
        return None
    try:

        # 构建鉴权对象
        q = Auth(access_key,secret_key)
        # 生成上传 Token，可以指定过期时间等
        token = q.upload_token(bucket_name)
        # 上传文件
        ret,info = put_data(token,None,data)

    except Exception as e:
        logging.error(e)
        raise e

    if not info and info.status_code != 200:
        raise Exception("文件上传失败")

    return ret['key']

if __name__ == '__main__':
    file_name=input('输入上传的文件')
    with open(file_name,'rb')as f:
        storage(f.read())

